/**
 * 
 */
package edu.gatech.oad.yuno;

import java.util.ArrayList;
import android.graphics.Color;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

/**
 * @author michael
 *
 */
public class TodoListAdapter extends ArrayAdapter<Todo> {
	private List<Todo> items;
    private Context context;
    
	public TodoListAdapter(Context context, int resource,
			int viewResourceId, List<Todo> objects) {
		super(context, resource, viewResourceId, objects);
		this.items = objects;
		this.context = context;
	}

	public TodoListAdapter(Context context, int resource,
			int viewResourceId, Todo[] objects) {
		super(context, resource, viewResourceId, objects);
		this.items = Arrays.asList(objects);
		this.context = context;
	}

	public TodoListAdapter(Context context, int resource, int viewResourceId) {
		super(context, resource, viewResourceId);
		this.items = new ArrayList<Todo>();
		this.context = context;
	}

	public TodoListAdapter(Context context, int viewResourceId,
			List<Todo> objects) {
		super(context, viewResourceId, objects);
		this.items = objects;
		this.context = context;
	}

	public TodoListAdapter(Context context, int viewResourceId, Todo[] objects) {
		super(context, viewResourceId, objects);
		this.items = Arrays.asList(objects);
		this.context = context;
	}

	public TodoListAdapter(Context context, int viewResourceId) {
		super(context, viewResourceId);
		this.items = new ArrayList<Todo>();
		this.context = context;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.todo_row, null);
        }
        //view.setBackgroundColor(Color.RED);
        Todo item = items.get(position);
        if (item != null) {
        	CheckBox rowComplete = (CheckBox) view.findViewById(R.id.rowComplete);
        	if (rowComplete != null) {
        		rowComplete.setChecked(item.isCompleted());
        	}
            TextView rowName = (TextView) view.findViewById(R.id.rowName);
            //rowName.setTextColor(Color.RED);
            if (rowName != null) {
                rowName.setText(item.getName());
                if (item.isCompleted())
                	rowName.setPaintFlags(rowName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                else
                	rowName.setPaintFlags(rowName.getPaintFlags() & ~(Paint.STRIKE_THRU_TEXT_FLAG));
            }
            TextView rowDate = (TextView) view.findViewById(R.id.rowDate);
            if (rowDate != null) {
            	 if(item.getDueDate() != null)
            		 rowDate.setText(DateHelper.format(item.getDueDate()));
            	 else
            		 rowDate.setText("");
            }
            TextView rowPriority = (TextView) view.findViewById(R.id.rowPriority);
            if (rowPriority != null) {
            	String text = "";
            	for (int i = 0; i < item.getPriority(); i++)
            		text += "*";
                rowPriority.setText(text);
            }
            TextView rowCategory = (TextView) view.findViewById(R.id.rowCategory);
            if (rowCategory != null) {
            	String text = "";
            	if (item.getCategory() != null) text += " #" + item.getCategory().getName();
            	//if (item.getLocation() != null) text += " @" + item.getLocation().getName();
                rowCategory.setText(text);
                if (item.getCategory()!=null){
                	System.out.println(item.getCategory());
                	//System.out.println(item.getCategory().getColor());
                	//System.out.println(item.getCategory().getColorInt());
                	rowCategory.setTextColor(item.getCategory().getColor());
                }
            }
         }

        return view;
    }
}
